package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.SearchTechnologyAdapter;
import com.digitalchina.lxnsy.api.BusinessTechnology;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TechnologyVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.xml.UserXML;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SearchTechnologyActivity extends BaseActivity {


    @Bind(R.id.search_technology_searchView)
    SearchView searchTechnologySearchView;
    @Bind(R.id.search_technology_lv)
    ListView searchTechnologyLv;

    private SearchTechnologyAdapter adapter;
    private List<TechnologyVO.QuestionBean> questionVOList = new ArrayList<>();


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_search_technology);
        setTitle("搜索问题");
    }

    @Override
    public void initMember() {
        adapter = new SearchTechnologyAdapter(this, questionVOList);
        searchTechnologyLv.setAdapter(adapter);
//        searchTechnologyLv.setTextFilterEnabled(true);
        //获取到TextView的ID
        int id = searchTechnologySearchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        //获取到TextView的控件
        TextView textView = (TextView) searchTechnologySearchView.findViewById(id);
        //设置字体大小为14sp
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);//14sp
    }

    @Override
    public void addListener() {

        searchTechnologySearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {

                if (StringUtil.isStrEmpty(query)) {
                    showToast("请输入关键字");
                    return false;
                }
                Intent intent = new Intent();
                intent.putExtra("query", query);
                setResult(RESULT_OK, intent);   //单选不需要裁剪，返回数据
                finish();
                return false;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)) {
                    BusinessTechnology.questionAuto(SearchTechnologyActivity.this, newText, "5", mHandler);
                } else {
                    adapter.clearListData();
                }
                return false;
            }
        });

        searchTechnologyLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (Utils.isLogin(SearchTechnologyActivity.this)) {
                    TechnologyVO technologyVO = new TechnologyVO();
                    TechnologyVO.PersonBeanX personBeanX = new TechnologyVO.PersonBeanX();
                    personBeanX.setUserAccId(UserXML.getUserId(SearchTechnologyActivity.this));
                    technologyVO.setQuestion(adapter.getListData().get(position));
                    technologyVO.setPerson(personBeanX);
                    GotoUtil.gotoActivity(SearchTechnologyActivity.this, AnswerListActivity.class, "TechnologyVO", technologyVO);
                } else
                    Utils.notLoginGoto(SearchTechnologyActivity.this);
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_QUESTION_AUTO_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_QUESTION_AUTO_FIELD:
                        LogUtils.e(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        questionVOList = FastJsonUtil.getListBean(object.toString(), "body", "questions", TechnologyVO.QuestionBean.class);


        if (questionVOList != null) {
            adapter.setListData(questionVOList);
        }

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
